package org.mcjug.oauth;

import oauth.signpost.OAuth;

import org.mcjug.oauth.gettokens.OauthActivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class GmailOauth extends Activity {

	SharedPreferences sp;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sp = PreferenceManager.getDefaultSharedPreferences(this);
		init();
	}

	private void init() {
		Button authenticateButton = (Button) findViewById(R.id.authenticateButton);
		authenticateButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), OauthActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});

		Button testSendButton = (Button) findViewById(R.id.sendEmailButton);
		testSendButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Email email = new Email("victor.semenov@gmail.com",
						"victor.semenov@gmail.com", "mooooo",
						"Enlarge your organ today!!!!!");
				Intent intent = new Intent(GmailOauth.this, GmailService.class);
				intent.putExtra(Constants.GMAIL_SERVICE_EMAIL_PARCELABLE_NAME,
						email);
				startService(intent);
			}
		});

		Button clearSpButton = (Button) findViewById(R.id.clearSpButton);
		clearSpButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Editor editor = sp.edit();
				editor.putString(OAuth.OAUTH_TOKEN, null);
				editor.putString(OAuth.OAUTH_TOKEN_SECRET, null);
				editor.commit();
				refreshTokenTextView();
			}
		});
	}

	private void refreshTokenTextView() {
		TextView titleTv = (TextView) findViewById(R.id.titleTextView);
		if (sp.getString(OAuth.OAUTH_TOKEN, null) != null) {

			String title = "Consumer Key: "
					+ sp.getString(OAuth.OAUTH_TOKEN, null)
					+ " Consumer Secret: "
					+ sp.getString(OAuth.OAUTH_TOKEN_SECRET, null);
			titleTv.setText(title);
		}
	}

	@Override
	protected void onPause() {
		unregisterReceiver(broadcastReciever);
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(broadcastReciever, new IntentFilter(
				Constants.EMAIL_FAILURE_INTENT_ACTION));
		registerReceiver(broadcastReciever, new IntentFilter(
				Constants.NO_TOKENS_FOR_EMAIL_INTENT_ACTION));
	}

	private static final BroadcastReceiver broadcastReciever = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(
					Constants.NO_TOKENS_FOR_EMAIL_INTENT_ACTION)) {
				Toast.makeText(context, "No tokens to send email!", 3000)
						.show();
			} else if (intent.getAction().equals(
					Constants.EMAIL_FAILURE_INTENT_ACTION)) {
				Toast.makeText(context, "Failure Sending email!", 3000).show();
			}
		}
	};
}